/*
 * Created by SharpDevelop.
 * User: hieutrung
 * Date: 4/25/2008
 * Time: 4:37 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Collections;

namespace QuickGenCode
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.ListBox lb;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.TextBox tb1;
		private System.Windows.Forms.Timer timer2;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox tb;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.Panel panel4;
		private System.Windows.Forms.NotifyIcon notifyIcon1;
		private System.Windows.Forms.ToolTip toolTip1;
		ListBox lbItem = new ListBox();
		
		private string sourceCodeFile = Application.StartupPath + "\\TemplateCode.txt";
		
		
		public MainForm(string _sourceCodeFile)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			if( _sourceCodeFile != null && _sourceCodeFile.Trim().Length > 0)
				sourceCodeFile = _sourceCodeFile;
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm( args.Length>0 ? args[0] : "" ));
		}
		
		#region keycode
		const int WM_SETHOTKEY = 50;
		const int WM_SHOWWINDOW = 24;
		const int HK_SHIFTA = 321;   // Shift + A
		const int HK_SHIFTB = 322;   // Shift + B
		const int HK_SHIFTZ = 346;	 // ALT + Z	
		const int HK_CONTROLA = 577; // Control + A
		const int HK_ALTZ = 1114;	 // ALT + Z	
		
			
		const int VK_Alt = 18;
		const int VK_Tab = 9;
		const int VK_Backspace = 8;		
		const int VK_Ctrl = 17;
		const int VK_Shift = 16;
		const int VK_V = 86;
		const int VK_A = 65;
		const int VK_B = 66;
		const int VK_Z = 90;
		
		const int KEYEVENTF_EXTENDEDKEY = 1;
		const int KEYEVENTF_KEYUP = 2;
		#endregion
		
		#region API functions
		[DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        
        [DllImport("user32.dll")]
        public static extern int ShowWindow(int hWnd, int nCmdShow);
        
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);	
        
        [DllImport("user32.dll")]
        public static extern int SetActiveWindow(int hWnd );
		
        [DllImport("user32.dll")]
        public static extern int SetFocus(int hWnd );
        
        [DllImport("user32.dll", EntryPoint="DefWindowProcA")]
   		private static extern int DefWindowProc(int hwnd, int wMsg, int wParam, int lParam);
		
   		[DllImport("user32.dll")]
    	private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
    	
    	[DllImport("user32.dll")]
    	private static extern int SetForegroundWindow(int hWnd );
    	
    	[DllImport("user32.dll")]
		private static extern int GetAsyncKeyState(int nVirtKey );
    	#endregion		
    	   	
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
			this.panel4 = new System.Windows.Forms.Panel();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.tb = new System.Windows.Forms.TextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.panel3 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.timer2 = new System.Windows.Forms.Timer(this.components);
			this.tb1 = new System.Windows.Forms.TextBox();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.lb = new System.Windows.Forms.ListBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.panel1.SuspendLayout();
			this.panel3.SuspendLayout();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// notifyIcon1
			// 
			this.notifyIcon1.ContextMenu = this.contextMenu1;
			this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
			this.notifyIcon1.Text = "Code Helper";
			this.notifyIcon1.Visible = true;
			this.notifyIcon1.Click += new System.EventHandler(this.formShow);
			// 
			// panel4
			// 
			this.panel4.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel4.Location = new System.Drawing.Point(0, 321);
			this.panel4.Name = "panel4";
			this.panel4.Size = new System.Drawing.Size(296, 5);
			this.panel4.TabIndex = 3;
			// 
			// checkBox1
			// 
			this.checkBox1.Dock = System.Windows.Forms.DockStyle.Left;
			this.checkBox1.Location = new System.Drawing.Point(72, 0);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(88, 24);
			this.checkBox1.TabIndex = 10;
			this.checkBox1.Text = "Top most";
			this.checkBox1.CheckedChanged += new System.EventHandler(this.CheckBox1CheckedChanged);
			// 
			// tb
			// 
			this.tb.BackColor = System.Drawing.SystemColors.Window;
			this.tb.Dock = System.Windows.Forms.DockStyle.Top;
			this.tb.Location = new System.Drawing.Point(0, 0);
			this.tb.Name = "tb";
			this.tb.Size = new System.Drawing.Size(245, 20);
			this.tb.TabIndex = 0;
			this.tb.Text = "";
			this.tb.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TbKeyDown);
			this.tb.TextChanged += new System.EventHandler(this.TbTextChanged);
			this.tb.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TbMouseUp);
			this.tb.Enter += new System.EventHandler(this.TbEnter);
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.lb);
			this.panel1.Controls.Add(this.tb);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(245, 326);
			this.panel1.TabIndex = 6;
			// 
			// splitter1
			// 
			this.splitter1.Dock = System.Windows.Forms.DockStyle.Right;
			this.splitter1.Location = new System.Drawing.Point(245, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 326);
			this.splitter1.TabIndex = 8;
			this.splitter1.TabStop = false;
			// 
			// panel3
			// 
			this.panel3.Controls.Add(this.panel4);
			this.panel3.Controls.Add(this.tb1);
			this.panel3.Dock = System.Windows.Forms.DockStyle.Right;
			this.panel3.Location = new System.Drawing.Point(248, 0);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(296, 326);
			this.panel3.TabIndex = 10;
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.checkBox1);
			this.panel2.Controls.Add(this.button6);
			this.panel2.Controls.Add(this.button5);
			this.panel2.Controls.Add(this.button4);
			this.panel2.Controls.Add(this.button3);
			this.panel2.Controls.Add(this.button2);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.Location = new System.Drawing.Point(0, 326);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(544, 24);
			this.panel2.TabIndex = 7;
			// 
			// timer2
			// 
			this.timer2.Interval = 10000;
			this.timer2.Tick += new System.EventHandler(this.Timer2Tick);
			// 
			// tb1
			// 
			this.tb1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tb1.Location = new System.Drawing.Point(0, 0);
			this.tb1.Multiline = true;
			this.tb1.Name = "tb1";
			this.tb1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.tb1.Size = new System.Drawing.Size(296, 326);
			this.tb1.TabIndex = 2;
			this.tb1.Text = "";
			this.tb1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Tb1MouseUp);
			this.tb1.Enter += new System.EventHandler(this.Tb1Enter);
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.menuItem1,
						this.menuItem2,
						this.menuItem3});
			// 
			// menuItem4
			// 
			this.menuItem4.Checked = true;
			this.menuItem4.Index = 0;
			this.menuItem4.Text = "Shift + Z";
			this.menuItem4.Click += new System.EventHandler(this.menu_hotkey);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "Shift + A";
			this.menuItem5.Click += new System.EventHandler(this.menu_hotkey);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 2;
			this.menuItem6.Text = "Shift + B";
			this.menuItem6.Click += new System.EventHandler(this.menu_hotkey);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.Text = "Show Windows";
			this.menuItem1.Click += new System.EventHandler(this.formShow);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.menuItem4,
						this.menuItem5,
						this.menuItem6,
						this.menuItem7});
			this.menuItem2.Text = "Hotkey";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 2;
			this.menuItem3.Text = "Exit";
			this.menuItem3.Click += new System.EventHandler(this.MenuItem3Click);
			// 
			// button4
			// 
			this.button4.Dock = System.Windows.Forms.DockStyle.Left;
			this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button4.ForeColor = System.Drawing.SystemColors.Control;
			this.button4.Image = ((System.Drawing.Image)(resources.GetObject("button4.Image")));
			this.button4.Location = new System.Drawing.Point(48, 0);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(24, 24);
			this.button4.TabIndex = 7;
			this.button4.Click += new System.EventHandler(this.Button4Click);
			// 
			// button5
			// 
			this.button5.Dock = System.Windows.Forms.DockStyle.Right;
			this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button5.ForeColor = System.Drawing.SystemColors.Control;
			this.button5.Image = ((System.Drawing.Image)(resources.GetObject("button5.Image")));
			this.button5.Location = new System.Drawing.Point(520, 0);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(24, 24);
			this.button5.TabIndex = 8;
			this.toolTip1.SetToolTip(this.button5, "Add new text-helper");
			this.button5.Click += new System.EventHandler(this.Button5Click);
			// 
			// button6
			// 
			this.button6.Dock = System.Windows.Forms.DockStyle.Right;
			this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button6.ForeColor = System.Drawing.SystemColors.Control;
			this.button6.Image = ((System.Drawing.Image)(resources.GetObject("button6.Image")));
			this.button6.Location = new System.Drawing.Point(496, 0);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(24, 24);
			this.button6.TabIndex = 9;
			this.toolTip1.SetToolTip(this.button6, "Remove selected text-helper");
			this.button6.Click += new System.EventHandler(this.Button6Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 3;
			this.menuItem7.Text = "Alt + Z";
			this.menuItem7.Click += new System.EventHandler(this.menu_hotkey);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(-144, 88);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(144, 23);
			this.button1.TabIndex = 9;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.Button1Click);
			// 
			// button2
			// 
			this.button2.Dock = System.Windows.Forms.DockStyle.Left;
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button2.ForeColor = System.Drawing.SystemColors.Control;
			this.button2.Image = ((System.Drawing.Image)(resources.GetObject("button2.Image")));
			this.button2.Location = new System.Drawing.Point(0, 0);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(24, 24);
			this.button2.TabIndex = 3;
			this.button2.Text = "button2";
			this.toolTip1.SetToolTip(this.button2, "Show/Hide code detail");
			this.button2.Click += new System.EventHandler(this.Button2Click);
			// 
			// button3
			// 
			this.button3.Dock = System.Windows.Forms.DockStyle.Left;
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button3.ForeColor = System.Drawing.SystemColors.Control;
			this.button3.Image = ((System.Drawing.Image)(resources.GetObject("button3.Image")));
			this.button3.Location = new System.Drawing.Point(24, 0);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(24, 24);
			this.button3.TabIndex = 6;
			this.toolTip1.SetToolTip(this.button3, "Reload code library");
			this.button3.Click += new System.EventHandler(this.Button3Click);
			// 
			// lb
			// 
			this.lb.ContextMenu = this.contextMenu1;
			this.lb.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lb.HorizontalScrollbar = true;
			this.lb.Location = new System.Drawing.Point(0, 20);
			this.lb.Name = "lb";
			this.lb.Size = new System.Drawing.Size(245, 303);
			this.lb.Sorted = true;
			this.lb.TabIndex = 1;
			this.lb.DoubleClick += new System.EventHandler(this.LbDoubleClick);
			this.lb.MouseUp += new System.Windows.Forms.MouseEventHandler(this.LbMouseUp);
			this.lb.SelectedIndexChanged += new System.EventHandler(this.LbSelectedIndexChanged);
			// 
			// timer1
			// 
			this.timer1.Interval = 50;
			this.timer1.Tick += new System.EventHandler(this.Timer1Tick);
			// 
			// MainForm
			// 
			this.AcceptButton = this.button1;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ClientSize = new System.Drawing.Size(544, 350);
			this.ContextMenu = this.contextMenu1;
			this.ControlBox = false;
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.panel3);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.panel2);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "MainForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Code Helper";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.MainFormClosing);
			this.Load += new System.EventHandler(this.MainFormLoad);
			this.Activated += new System.EventHandler(this.MainFormActivated);
			this.Deactivate += new System.EventHandler(this.formHide);
			this.panel1.ResumeLayout(false);
			this.panel3.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);
		}
		#endregion
		void Button1Click(object sender, System.EventArgs e)
		{
			LbDoubleClick(null, new EventArgs());
		}
		
		
		
		void MainFormLoad(object sender, System.EventArgs e)
		{ 
			Button2Click(null, new EventArgs());
			this.Location = new Point(this.Location.X + (this.tb1.Width/2),this.Location.Y);
			reloadData();
			lbItem.Sorted = true;
			
			//listen WINDOW + SPACE
			//timer1.Start();		
						
			loadConfig();
		} 
		
		public void reloadData(){
			lb.Items.Clear();
			lbItem.Items.Clear();
			if(!File.Exists(sourceCodeFile)){
				StreamWriter sw = new StreamWriter(sourceCodeFile);
				sw.Write(">>>DemoCode<<<\r\n<div id=\"Demo\"></div>");
				sw.Flush();
				sw.Close();
			}
			
			StreamReader sr = new StreamReader(sourceCodeFile);
			string content = sr.ReadToEnd().Replace(">>>", (char)(128)+"");
			string[] itemCode = content.Split( (char)(128) );			
			
			foreach(string item in itemCode){
				if(item.Trim().Length>0){
					lbItem.Items.Add( item );
					lb.Items.Add(item.Replace("<<<", (char)(128)+"").Split( (char)(128) )[0] );					
				}
			}
			sr.Close();
		}
		
		void LbSelectedIndexChanged(object sender, System.EventArgs e)
		{			
			
			tb1.Text = (lbItem.Items[ lbItem.FindString( lb.SelectedItem +"<<<" ) ].ToString().Replace("<<<", (char)(128)+"").Split( (char)(128) )[1]).Trim();
			
		}
		
		void LbDoubleClick(object sender, System.EventArgs e)
		{
			doInsertText(false);			
		}
		
		void doInsertText(bool backspace){
			Clipboard.SetDataObject( tb1.Text, true );
			
			//Alt + Tab
			keybd_event (VK_Alt	, 0, 0, 0);
			keybd_event (VK_Tab , 0, 0, 0);
			
			keybd_event (VK_Alt	, 0, KEYEVENTF_KEYUP, 0);
			keybd_event (VK_Tab	, 0, KEYEVENTF_KEYUP, 0);		
			
			
			//Backspace
			if(backspace){
				keybd_event (VK_Backspace , 0, 0, 0);
				keybd_event (VK_Backspace	, 0, KEYEVENTF_KEYUP, 0);
			}
			
			//Ctrl + V
			keybd_event (VK_Ctrl, 0, 0, 0);
			keybd_event (VK_V   , 0, 0, 0);
			
			keybd_event (VK_Ctrl, 0, KEYEVENTF_KEYUP, 0);
			keybd_event (VK_V, 0, KEYEVENTF_KEYUP, 0);
			
			formHide(null, new EventArgs());
		}
				
		
		void Button2Click(object sender, System.EventArgs e)
		{
			if(panel3.Visible){				
				this.ClientSize = new System.Drawing.Size(lb.Width, this.ClientSize.Height);	
				splitter1.Visible = panel3.Visible = button5.Visible = button6.Visible = false;
			}else{				
				this.ClientSize = new System.Drawing.Size(lb.Width+tb1.Width, this.ClientSize.Height);
				splitter1.Visible = panel3.Visible = button5.Visible = button6.Visible = true;
			}
			
		}
		
		void TbTextChanged(object sender, System.EventArgs e)
		{
			if(lb.FindString(tb.Text) >=0)
				lb.SelectedIndex = lb.FindString(tb.Text);
			
		}
		
		void TbEnter(object sender, System.EventArgs e)
		{
			tb.SelectAll();
		}	
		
		
		void MainFormActivated(object sender, System.EventArgs e)
		{
			this.Opacity = 1;
			tb.Select();
			tb.SelectAll();			
		}		
		
		void Button3Click(object sender, System.EventArgs e)
		{
			reloadData();
		}
		
		void Button4Click(object sender, System.EventArgs e)
		{
			Process.Start(sourceCodeFile);
		}
		
		void Button5Click(object sender, System.EventArgs e)
		{
			if( tb.Text.Trim().Length == 0 )
				MessageBox.Show("Name of text-helper - too short.");
			else if( tb1.Text.Trim().Length == 0 )
				MessageBox.Show("Name of text-helper - too short.");
			else if( lb.FindStringExact(tb.Text.Trim()+"<<<") >=0 ){
				if(MessageBox.Show("Name of text-helper already existed. Do you want to overwrite this item?","Confirm",MessageBoxButtons.OKCancel).ToString() == "OK"){
					RemoveItem( lbItem.Items[lbItem.FindString(tb.Text + "<<<")].ToString() );
					AddItem( tb.Text, tb1.Text );				
					reloadData();
					TbTextChanged(null, new EventArgs());
					tb1.Text = "";
				}
			}
			else
			{
				AddItem( tb.Text, tb1.Text );				
				reloadData();
				TbTextChanged(null, new EventArgs());
				tb1.Text = "";
				
			}
		}
		
		void Button6Click(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("Remove this text-helper? [Name: "+lb.SelectedItem +"]","Confirm",MessageBoxButtons.OKCancel).ToString().Equals("OK"))
			{
				RemoveItem( lbItem.Items[lb.SelectedIndex].ToString() );
				tb.Text = tb1.Text = "";
				reloadData();
			}
		}
		
		public void AddItem(string title, string content){
			StreamReader sr = new StreamReader(sourceCodeFile);
			string contentFile = sr.ReadToEnd();
			sr.Close();
			
			StreamWriter sw = new StreamWriter(sourceCodeFile);
			sw.Write(contentFile + "\r\n>>>"+title+"<<<\r\n"+content+"\r\n");
			sw.Flush();
			sw.Close();
		}
		
		public void RemoveItem(string item){
			StreamReader sr = new StreamReader(sourceCodeFile);
			string content = sr.ReadToEnd();
			sr.Close();
			
			content = content.Replace( ">>>" + item, "" );
			
			StreamWriter sw = new StreamWriter(sourceCodeFile);
			sw.Write(content);
			sw.Flush();
			sw.Close();		
			
		}
		
		
		
		void Tb1Enter(object sender, System.EventArgs e)
		{			
			tb1.SelectAll();
		}
		
		void Tb1MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			tb1.SelectAll();
		}
		
		void TbMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			tb.SelectAll();
		}
		
		void TbKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			toolTip1.SetToolTip(tb, e.KeyValue+"");
			if(e.KeyValue == 40 && lb.SelectedIndex+1 < lb.Items.Count)
				lb.SelectedIndex++;
			
			if(e.KeyValue == 38 && lb.SelectedIndex > 0)
				lb.SelectedIndex--;
			
			if(e.KeyValue == 27) formHide( this, new EventArgs() );
			
		}
		
		
		
		void LbMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(lb.Items.Count > 0 && lb.SelectedIndex >= 0)
				tb.Text = lb.SelectedItem.ToString();
		}
		 
		void Timer1Tick(object sender, System.EventArgs e)
		{
			if(GetAsyncKeyState(91).ToString().IndexOf("-")>=0 && GetAsyncKeyState(32).ToString( ).IndexOf("-")>=0){			
				formShow(null, new EventArgs());
			}
			
			if(GetAsyncKeyState(27).ToString().IndexOf("-")>=0)
				formHide(null, new EventArgs());
		}
				
		
		void MenuItem2Click(object sender, System.EventArgs e)
		{			
			this.Close();
		} 		
		
		void formShow(object sender, System.EventArgs e)
		{
			showFormByHotKey();
		}
		
		
		public void showFormByHotKey(){			
			if(hotkey == "Alt + Z"){				
				keybd_event (VK_Alt, 0, 0, 0);
				keybd_event (VK_Z, 0, 0, 0);
				keybd_event (VK_Z, 0, KEYEVENTF_KEYUP, 0);
				keybd_event (VK_Alt, 0, KEYEVENTF_KEYUP, 0);				
			}
			if(hotkey == "Shift + A"){
				keybd_event (VK_Shift, 0, 0, 0);
				keybd_event (VK_A, 0, KEYEVENTF_EXTENDEDKEY, 0);
				keybd_event (VK_A, 0, KEYEVENTF_KEYUP, 0);
				keybd_event (VK_Shift, 0, KEYEVENTF_KEYUP, 0);
			}
			if(hotkey == "Shift + B"){
				keybd_event (VK_Shift, 0, 0, 0);
				keybd_event (VK_B, 0, KEYEVENTF_EXTENDEDKEY, 0);
				keybd_event (VK_B, 0, KEYEVENTF_KEYUP, 0);
				keybd_event (VK_Shift, 0, KEYEVENTF_KEYUP, 0);
			}
			if(hotkey == "Shift + Z"){
				keybd_event (VK_Shift, 0, 0, 0);
				keybd_event (VK_Z, 0, KEYEVENTF_EXTENDEDKEY, 0);
				keybd_event (VK_Z, 0, KEYEVENTF_KEYUP, 0);
				keybd_event (VK_Shift, 0, KEYEVENTF_KEYUP, 0);
			}
		}
		
		public void showFormByKey(){			
			this.Show();			
			this.SetTopLevel(true);
			this.Activate();
			
			//Alt + Tab
			keybd_event (VK_Alt	, 0, 0, 0);
			keybd_event (VK_Tab , 0, 0, 0);
			
			keybd_event (VK_Alt	, 0, KEYEVENTF_KEYUP, 0);
			keybd_event (VK_Tab	, 0, KEYEVENTF_KEYUP, 0);
			
			this.SetTopLevel(true);
			this.Activate();
			
		}
		
		void formHide(object sender, System.EventArgs e){
			if(!checkBox1.Checked){
				this.Hide();
				this.Show();
				this.SendToBack();
				this.Opacity = 0;
				//this.Text = DateTime.Now.ToLongTimeString();
			}
		}
		
		void CheckBox1CheckedChanged(object sender, System.EventArgs e)
		{
			this.TopMost = checkBox1.Checked;
		}
		
		
		void MainFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{		
			
		}
		
		bool isforceExist = false;
		void Timer2Tick(object sender, System.EventArgs e)
		{
			isforceExist = true;
			this.Close();
		}
		
		void MenuItem3Click(object sender, System.EventArgs e)
		{
			isforceExist = true;
			this.Close();
		}
		
		string hotkey = "Alt + Z";
		void menu_hotkey(object sender, System.EventArgs e)
		{
			MenuItem menu = ((MenuItem)sender);		
			hotkey = menu.Text;			
			setHotKey(hotkey);
			saveConfig();
		}
		
		string path     = "Software\\Poptato softwares\\QuickGenCode";
		string keyName  = "Hotkey";		
		public void loadConfig(){
			hotkey = getRegistryKey(path,keyName) != null ? getRegistryKey(path,keyName) : hotkey;			
			setHotKey(hotkey);
		}
		
		public void setHotKey(string hotkeyValue){			
			int hWnd = FindWindow( null, this.Text );			
			DefWindowProc( hWnd , WM_SHOWWINDOW, 0, 0);
			menuItem4.Checked = menuItem5.Checked = menuItem6.Checked = menuItem7.Checked = false;			
			
			if(hotkeyValue == "Shift + Z"){	SendMessage( hWnd, WM_SETHOTKEY, HK_SHIFTZ, 0); menuItem4.Checked = true;}
			if(hotkeyValue == "Shift + A"){ SendMessage( hWnd, WM_SETHOTKEY, HK_SHIFTA, 0); menuItem5.Checked = true;}
			if(hotkeyValue == "Shift + B"){ SendMessage( hWnd, WM_SETHOTKEY, HK_SHIFTB, 0); menuItem6.Checked = true;}
			if(hotkeyValue == "Alt + Z")  {	SendMessage( hWnd, WM_SETHOTKEY, HK_ALTZ, 0); menuItem7.Checked = true;}
			
			this.Text = "Code Helper ("+hotkeyValue+")";
			this.notifyIcon1.Text = this.Text;
		}
		
		public void saveConfig(){
			setRegistryKey(path, keyName, hotkey );
		}
		
		public bool setRegistryKey(string path, string key, string val){
			try{
		        RegistryKey rk = Registry.CurrentUser;
		        RegistryKey sk1 = rk.CreateSubKey(path);
		        sk1.SetValue(key.ToUpper(), val);
		        return true;
		    }
		    catch (Exception e)
		    {   	
		        MessageBox.Show( e + "\r\nWriting registry " + key.ToUpper());
		        return false;
		    }
		}
		
		public string getRegistryKey(string path, string key){
			RegistryKey rk = Registry.CurrentUser ;		
		    RegistryKey sk1 = rk.OpenSubKey(path);		   		
		    if ( sk1 == null ) return null;		    
		    else
		    {
		    	try{
		            return (string)sk1.GetValue(key.ToUpper());
		        }catch (Exception e){		            
		            MessageBox.Show( e + "\r\nReading registry " + key.ToUpper());
		        	return null;
		        }
		    }
		}
	}
}
 
